package com.ruoyi.back.service.impl

import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.app.common.date
import com.ruoyi.back.domain.dto.ExtractVideoQueryDto
import com.ruoyi.back.domain.v3.ExtractVideo
import com.ruoyi.back.mapper.ExtractVideoMapper
import com.ruoyi.back.service.ExtractVideoService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.stereotype.Service

@Service
class ExtractVideoServiceImpl : ServiceImpl<ExtractVideoMapper, ExtractVideo>(), ExtractVideoService {

    override fun saveExtractVideo(extractVideo: ExtractVideo): Data<Boolean> =
        extractVideo.id?.run {
            Ok("更新成功", updateById(extractVideo))
        } ?: kotlin.run {
            Ok("保存成功", save(extractVideo))
        }

    override fun deleteExtractVideo(ids: String): Data<Boolean> =
        Ok("批量移除成功", removeByIds(ids.split(",")))

    override fun extractVideoList(extractVideoQueryDto: ExtractVideoQueryDto): Page<ExtractVideo> =
        TableSupport.buildPageRequest().let { pageDomain ->
            page(
                Page(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong()),
                KtQueryWrapper(ExtractVideo::class.java)
                    .like(
                        StrUtil.isNotBlank(extractVideoQueryDto.title),
                        ExtractVideo::title,
                        extractVideoQueryDto.title
                    )
                    .gt(
                        StrUtil.isNotBlank(extractVideoQueryDto.beginDate),
                        ExtractVideo::createTime,
                        extractVideoQueryDto.beginDate.date()
                    )
                    .lt(
                        StrUtil.isNotBlank(extractVideoQueryDto.endDate),
                        ExtractVideo::createTime,
                        extractVideoQueryDto.endDate.date()
                    )
                    .orderByDesc(ExtractVideo::createTime)
            )
        }

}